var express = require("express");
var router = express.Router();
var mongoose = require("mongoose");
var goods = require("../models/goods");
var uuid = require('uuid/v1');

//连接数据库  27017 mongodb默认端口 后边是自己的数据库名字
mongoose.connect("mongodb://127.0.0.1:27017/zp_project");

//监听数据库是否连接成功
mongoose.connection.on("connected", function() {
	console.log("数据库连接成功！！！！");
});

mongoose.connection.on("error", function() {
	console.log("数据库连接失败！！");
});

//监听数据库是否断开
mongoose.connection.on("disconnected", function() {
	console.log("数据库已断开！！");
});

//商品列表
router.get('/goodsList', function(req, res, next) {
	var searchGoodsName = req.query.searchGoodsName;  
	var pageSize = parseInt(req.query.pageSize);
	var page = parseInt(req.query.page);
	var sortFlag = req.query.sort;
	var priceFilterStart = req.query.priceFilterStart;
	var priceFilterEnd = req.query.priceFilterEnd;
	// 跳过几条数据查询
	var skip = (page - 1) * pageSize;
	
	//查询条件
	var findCondition = {};
	// mongo 中的模糊查询其实就是正则匹配， 在mongo中可以使用 '/.../'来直接查询 ， 但是在 express中 不能这样使用 必须要 new RegExp()来构建正则的表达对象
	if (searchGoodsName != "") {
		findCondition = {
			"productName" : new RegExp(searchGoodsName) 
		}
	}
	if (priceFilterStart != "" && priceFilterEnd != "") {
		findCondition.productDefaultPrice = {$gte : priceFilterStart, $lt : priceFilterEnd}
	}
	
	goods.find(findCondition, function (err, goodsList) {
		if(err) {
			res.json({
				status: 1,
				message: err.message,
				result: ""
			});
		} else {
			if(goodsList) {
				var goodsDataLength = goodsList.length;
				var goodsDataArr = goodsList.splice(skip, pageSize);
				//对查询的数据排序
				function arrObjSort (item, sortFlag) {
					if (sortFlag == 1) {
						return function (a, b) {
							var value_1 = a[item];
							var value_2 = b[item];
							return value_1 - value_2;
						}
					}else if (sortFlag == -1){
						return function (a, b) {
							var value_1 = a[item];
							var value_2 = b[item];
							return value_2 - value_1;
						}
					}
				}
				goodsDataArr.sort(arrObjSort("productDefaultPrice", sortFlag));
				res.json({
					status: 0,
					message: "",
					result: {
						goodsListData: goodsDataArr,
						total: goodsDataLength
					}
				});
			}
		}
	});
});


//商品搜索
router.get("/searchGoods", function (req, res, next) {
	var searchGoodsName = req.query.searchGoodsName;  
	var pageSize = parseInt(req.query.pageSize);
	var page = parseInt(req.query.page);
	var sortFlag = req.query.sort;
	var priceFilterStart = req.query.priceFilterStart;
	var priceFilterEnd = req.query.priceFilterEnd;
	// 跳过几条数据查询
	var skip = (page - 1) * pageSize;
	
	var findCondition = {};
	// mongo 中的模糊查询其实就是正则匹配， 在mongo中可以使用 '/.../'来直接查询 ， 但是在 express中 不能这样使用 必须要 new RegExp()来构建正则的表达对象
	if (searchGoodsName != "") {
		findCondition = {
			"productName" : new RegExp(searchGoodsName) 
		}
	}
	if (priceFilterStart != "" && priceFilterEnd != "") {
		findCondition.productDefaultPrice = {$gte : priceFilterStart, $lt : priceFilterEnd}
	}
	goods.find(findCondition , function (err, goodsData) {
		if(err) {
			res.json({
				status: 1,
				message: err.message,
				result: ""
			});
		} else {
			if(goodsData) {
				var goodsDataLength = goodsData.length;
				var goodsDataArr = goodsData.splice(skip, pageSize);
				//对查询的数据排序
				function arrObjSort (item, sortFlag) {
					if (sortFlag == 1) {
						return function (a, b) {
							var value_1 = a[item];
							var value_2 = b[item];
							return value_1 - value_2;
						}
					}else if (sortFlag == -1){
						return function (a, b) {
							var value_1 = a[item];
							var value_2 = b[item];
							return value_2 - value_1;
						}
					}
				}
				goodsDataArr.sort(arrObjSort("productDefaultPrice", sortFlag));
				res.json({
					status: 0,
					message: "",
					result: {
						goodsListData: goodsDataArr,
						total: goodsDataLength
					}
				});
			}
		}
	})
});

module.exports = router;
